<template>
  <div class="playMusic">
    <div
      class="bg"
      :style="{ backgroundImage: `url(${playDetail.al.picUrl})` }"
    ></div>
    <div class="playTop">
      <div class="back" @click="changeShow">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-arrowLeft"></use>
        </svg>
      </div>
      <div class="center">
        <div class="title">
          <marquee :behavior="srcoll" direction="left">
            {{ playDetail.al.name }}
          </marquee>
        </div>
      </div>
      <div class="share">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-fenxiang"></use>
        </svg>
      </div>
    </div>
    <div class="playContent" v-show="!isLyric">
      <img
        :class="['needle', paused ? '' : 'active']"
        src="@/assets/img/song4.jpg"
        alt=""
      />
      <img class="disc" src="@/assets/img/song3.jpg" alt="" />
      <img class="playImg" :src="playDetail.al.picUrl" alt="" />
    </div>
    <div class="playLyric" v-show="isLyric" ref="playLyric">
      <p
        v-for="(item, index) in $store.getters.lyricList"
        :key="index"
        :class="[handleTime(item) ? 'active' : '']"
      >
        {{ item.lyric }}
      </p>
    </div>
    {{ lyricList }}
    <div class="progress"></div>
    <div class="playFooter">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-xunhuan"></use>
      </svg>
      <svg class="icon" aria-hidden="true" @click="goPlay(-1)">
        <use xlink:href="#icon-shangyishoushangyige"></use>
      </svg>
      <svg class="icon play" aria-hidden="true" v-if="paused" @click="isPlay">
        <use xlink:href="#icon-bofang1"></use>
      </svg>
      <svg class="icon play" aria-hidden="true" v-else @click="isPlay">
        <use xlink:href="#icon-tingzhi1"></use>
      </svg>
      <svg class="icon" aria-hidden="true" @click="goPlay(1)">
        <use xlink:href="#icon-048caozuo_xiayishou"></use>
      </svg>
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-bofangliebiao"></use>
      </svg>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "playMusic",
  components: {},
  props: ["playDetail", "paused"],
  data() {
    return {
      isLyric: true,
    };
  },
  computed: {
    ...mapState([
      "lyric",
      "lyricList",
      "currentTime",
      "playlist",
      "playCurrentIndex",
    ]),
  },
  methods: {
    changeShow() {
      this.$emit("controllerShow");
    },

    isPlay() {
      this.$emit("play");
    },

    handleTime(item) {
      if (!isNaN(item.time)) {
        if (item.time == 0 && this.currentTime == 0) {
          return true;
        } else {
          let time = parseInt((this.currentTime * 1000).toString());
          if (item.preTime <= time && time < item.time) {
            return true;
          }
        }
      }
      return false;
    },

    goPlay(num) {
      let list = this.playlist.tracks;
      let index = this.playCurrentIndex + num;
      if (index < 0) {
        index = list.length - 1;
      } else if (list.length == index) {
        index = 0;
      }
      this.$store.commit("setPlayIndex", index);
    },
  },
  watch: {
    currentTime: function (newVal, oldVal) {
      let p = document.querySelector("p.active");
      this.$refs.playLyric.scrollTop = p.offsetTop;
    },
  },
};
</script>
<style lang="less" scoped>
.playMusic {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  .bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-size: auto 100%;
    filter: blur(60px);
    background-position: center;
    z-index: -1;
  }
  .playTop {
    display: flex;
    width: 7.5rem;
    height: 1.2rem;
    position: absolute;
    left: 0;
    top: 0;
    color: #000;
    justify-content: space-between;
    align-items: center;
    padding: 0 0.4rem;
    .icon {
      width: 0.4rem;
      height: 0.4rem;
      fill: #000;
    }
    marquee {
      width: 5rem;
    }
  }
  .playContent {
    position: relative;
    width: 7.5rem;
    height: 7.5rem;
    left: 0;
    top: 1.5rem;

    .needle {
      width: 1.6rem;
      height: auto;
      position: absolute;
      left: 50%;
      transform: translateX(-25%) rotate(-25deg);
      z-index: 10;
      transform-origin: 0 0;
      transition: all 1s;
    }
    .needle.active {
      transform: translateX(-25%) rotate(0deg);
    }
    .disc {
      width: 5.5rem;
      height: auto;
      position: absolute;
      left: 50%;
      top: 1.5rem;
      transform: translateX(-50%);
    }
    .playImg {
      width: 3.5rem;
      height: 3.5rem;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      top: 2.5rem;
      transform: translateX(-50%);
    }
  }
  .playLyric {
    position: absolute;
    width: 7.5rem;
    height: 9rem;
    left: 0;
    top: 1.5rem;
    overflow: scroll;
    text-align: center;
    color: #fff;
    padding: 0.2rem;
    .active {
      color: red;
    }
  }
  .playFooter {
    width: 7.5rem;
    height: 1.5rem;
    position: fixed;
    display: flex;
    justify-content: space-between;
    align-items: center;
    left: 0;
    bottom: 0;
    padding: 0 0.2rem;
    .icon {
      width: 0.5rem;
      height: 0.5rem;
      fill: #fff;
      &.play {
        width: 0.8rem;
        height: 0.8rem;
      }
    }
  }
}
</style>